import { defineStore } from 'pinia'
// @ts-ignore
import { ElMessage } from 'element-plus'
import {getCurrentInstance} from "vue";
// @ts-ignore
import router from "../../modules/router.ts";

export default defineStore('storeIdF', {
    // 其他配置...
    state: ()=>{
        return{
            list: [],
            searchListA: [],
            // 已选择的id
            keyArr: []
        }
    },
    getters: {
    },
    // 同步异步统一到actions中了
    actions: {
        // alterA(val: string): void {
        //     this.radio = val
        //     console.log(this.radio)
        // },
        // 添加
        add(obj: any){
            const arr = {
                AccountNumber: obj.AccountNumber,
                name: obj.name,
                number: obj.number,
                Authority: obj.Authority,
                Position: obj.Position,
                password: obj.password,
                time: obj.time,
                key: this.list.length+1
            }
            //@ts-ignore
            this.list.push(arr)
            this.list = this.list.reduce((acc, obj:any) => {
                // console.log(obj)
                const existingObj = acc.find((item:any) => JSON.stringify(`${item.AccountNumber}`) === JSON.stringify(`${obj.AccountNumber}`));
                console.log(existingObj)
                if (!existingObj) {
                    //@ts-ignore
                    acc.push(obj);
                }
                return acc;
            }, []);
            this.searchListA = this.list
        },
        // 数据请求
        async request() {
            let {proxy}: any = getCurrentInstance()
            let {data: {Authority}} = await proxy.$axios.get('/json/evaluate.json')
            this.list = Authority
            this.searchListA = this.list
            console.log(Authority)
        },
        // 修改
        editA(obj: any, key: number) {
            this.list.some((item: any) => {
                if (item.key === key) {
                    item.AccountNumber = obj.AccountNumber
                    item.name = obj.name
                    item.number = obj.number
                    item.Position = obj.Position
                    item.Authority = obj.Authority
                    item.time = obj.time
                    item.password = obj.password
                    return true
                }
            })
        },
        // 搜索功能
        searchA(val: any) {
            console.log(val)
            if (val === null || val === '') {
                this.searchListA = this.list
            } else {
                // 筛选出数据赋给searchList
                this.searchListA = this.list.filter((item: any) => item.name === val || item.AccountNumber === val)
            }
        },
        // 重置密码
        reset(str:string){
            console.log(111111)
            this.list.some((item:any)=>{
                if(item.AccountNumber === localStorage.getItem('token')){
                    item.password = str
                    router.push({
                        path: '/LogIn'
                    })
                    ElMessage({
                        message: '修改成功，请从新登录',
                        type: 'success',
                    })
                    return true
                }
            })
        },
        // 存储已选数据
        keyArrA(obj: any) {
            this.keyArr = obj
        },
        // 批量删除
        batch():void{
            var Current:number = 0;
            this.list.some((item:any)=> {
                console.log(localStorage.getItem('token'),'====',item.name)
                if(item.AccountNumber === localStorage.getItem('token')){
                    Current = item.key
                    return true
                }
            })
            this.keyArr.forEach((itemKey:any):void=>{
                if (itemKey === Current) {
                    ElMessage({
                        message: '不能删除当前账户，只能修改密码，或者到注销页进行操作',
                        type: 'warning'
                    })
                    return;
                }
                this.list = this.list.filter((item:any):boolean=>{
                    if(item.key === itemKey){
                        ElMessage({
                            message: `账户${item.AccountNumber}删除成功`,
                            type: 'success'
                        })
                    }
                    // if(item.key)
                    return item.key !== itemKey
                })
                this.searchListA = this.list
            })
        }
    }
});

